package xujiwen.transport.Mgr;

import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.CarInfo;
import xujiwen.transport.util.DBConnect;

public class CarInfoMgr {
	public void Save(final CarInfo car) {

		final DBConnect db = new DBConnect();
		final String sql = "insert into CARINFO values (?, ?, ?, ?, ?, now())";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			
			db.setString(1, car.getCid());
			db.setString(2, car.getOwnerId());
			db.setString(3, car.getCarType());
			db.setInt(4, car.getCarClass());
			db.setInt(5, car.getLoading());
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	/*public CarInfo GetDeatilUser(final int oid) {
		CarInfo car = null;
		final DBConnect db = new DBConnect();
		final String sql = "select * from CARINFO where OWNERID = ?";
		db.prepareStatement(sql);
		db.setInt(1, oid);
		db.excuteQuery();
		try {
			while (db.next()) {
				car = new CarInfo();
				car.setOwnerId(db.getString("OWNERID"));
				car.setName(db.getString("NAME"));
				car.setPhone(db.getString("PHONE"));
				car.setSex(db.getString("SEX"));
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return car;
	}
	*/
	public List<CarInfo> GetSearchCarList(String str) {
		final List<CarInfo> cars = new ArrayList<CarInfo>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from CARINFO where CID like '%" + str +"%' or OWNERID like '%" + str +"%'";
		else sql = "select * from CARINFO limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final CarInfo car = new CarInfo();
				car.setCid(db.getString("CID"));
				car.setOwnerId(db.getString("OWNERID"));
				car.setCarType(db.getString("CARTYPE"));
				car.setCarClass(db.getInt("CARCLASS"));
				car.setLoading(db.getInt("LOADING"));
				car.setRecordDate(db.getTimestamp("RECORDDATE"));
				cars.add(car);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return cars;
	}
	
	public void Del(final String cid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from CARINFO where CID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, cid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
